package com.iot.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MyDateTime {
    public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String TIME_PATTERN = "HH:mm:ss";


    public static final  SimpleDateFormat DateTimeFM=new SimpleDateFormat(DATETIME_PATTERN);
    public static final SimpleDateFormat DateFM=new SimpleDateFormat(DATE_PATTERN);

    public  static  String toString(Date dt){
         return  DateTimeFM.format(dt);
    }
    public  static  String toShortString(Date dt){
        return  DateFM.format(dt);
    }
    public  static  String toHMString(Date dt){
        String r=toString(dt);
        Long hm=dt.getTime()%1000;
        return r+String.format(".%3d",hm);

    }

    public  static  Date parse(String s) throws Exception   {
        if(s.length()==10){
            return DateFM.parse(s);
        }
        return DateTimeFM.parse(s);
    }
    public  static  String today(){
        Date d=new Date();
        return toShortString(d);
    }
    public  static  String todayShort(){
        Date d=new Date();
        return toShortString(d).replace("-","");
    }
    public  static  String now(){
        Date d=new Date();
        return toString(d);
    }
    public static Date addDay(Date dt,int day){
        Calendar   calendar = new GregorianCalendar();
        calendar.setTime(dt);
        calendar.add(calendar.DATE,day); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }
    public static Date addSecond(Date dt,int addv){
        Calendar   calendar = new GregorianCalendar();
        calendar.setTime(dt);
        calendar.add(calendar.SECOND,addv); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }
}
